﻿using System;
using System.Linq;
using Enyim.Caching;
using Web.OnlinePay;
using Web.Controllers;
using System.Web.Mvc;
using Web.ActionFilters;
using SilenthinkContract;
using System.Collections.Generic;
using SilenthinkContract.Domain;
using SilenthinkCommon.Extension;
using SilenthinkContract.Component;
using Web.VirtualProductHandler;

namespace Web.Areas.Pay.Controllers
{
    [SkAuthorize]
    public abstract class PayBaseController : BaseController
    {
        protected PayBaseController(ILogService logService, IBaseService baseService, IExtraService extraService, IStoreService storeService, IOrderService orderService, IGameService gameService, IAuctionService auctionService, IPassportService passportService, IPaymentService paymentService, IMemcachedClient memcachedClient)
            : base(logService, baseService, extraService, storeService, orderService, gameService, auctionService, passportService, paymentService, memcachedClient)
        {
        }
        [NonAction]
        protected IList<RechargePackage> GetRechargePackages(long userId)
        {
            var now = DateTime.Now;
            var list = new List<RechargePackage>();
            var packages = PaymentService.GetAllRechargePackage(true);
            foreach (var package in packages)
            {
                if (package.BeginDate.HasValue)
                {
                    if (package.BeginDate >= now)
                    {
                        if (package.EndDate.HasValue)
                        {
                            if (package.EndDate <= now)
                            {
                                if (ValidatePackage(package, userId))
                                {
                                    list.Add(package);
                                }
                            }
                        }
                        else
                        {
                            if (ValidatePackage(package, userId))
                            {
                                list.Add(package);
                            }
                        }
                    }
                }
                else
                {
                    if (package.EndDate.HasValue)
                    {
                        if (package.EndDate <= now)
                        {
                            if (ValidatePackage(package, userId))
                            {
                                list.Add(package);
                            }
                        }
                    }
                    else
                    {
                        if (ValidatePackage(package, userId))
                        {
                            list.Add(package);
                        }
                    }
                }
            }
            return list;
        }

        [NonAction]
        private bool ValidatePackage(RechargePackage package, long userId)
        {
            if (package.Limitation == PackageLimitation.NTimesOnly)
            {
                return package.UsedCount < package.LimitationTimes && ValidatePackageInternal(package, userId);
            }
            if (package.Limitation == PackageLimitation.NTimesPerCustomer)
            {
                var count = PaymentService.GetUserUsedPackageCount(userId, package.Id);
                return count < package.LimitationTimes && ValidatePackageInternal(package, userId);
            }
            return ValidatePackageInternal(package, userId);
        }

        [NonAction]
        private bool ValidatePackageInternal(RechargePackage package, long userId)
        {
            if (package.Requirements != null && package.Requirements.Any(o => o.Status))
            {
                return package.Requirements.All(requirement => HandlerPackageRequirement(requirement, userId));
            }
            return true;
        }

        private bool HandlerPackageRequirement(RechargePackageRequirement requirement, long userId)
        {
            if (requirement.Status)
            {
                if (requirement.Id == 1) //首次充值
                {
                    return PaymentService.GetUserRechargeCount(userId, 0) == 0;
                }
            }
            return false;
        }
        [NonAction]
        protected IList<PaymentPlatform> GetAllPaymentPlatform()
        {
            return MemcachedClient.Put(CacheKey.Base.PaymentPlatforms, () => PaymentService.GetAllPaymentPlatform(true));
        }
        [NonAction]
        protected IList<PaymentEBank> GetAllPaymentEBank()
        {
            return MemcachedClient.Put(CacheKey.Base.EBanks, () => PaymentService.GetAllPaymentEBank(null, true));
        }

        [NonAction]
        protected void InternalHandler(PayTrade payTrade)
        {
            var trade = PaymentService.GetUserTrade(payTrade.AssociatedId, payTrade.UserId);
            if (trade.Paid == trade.Amount)
            {
                if (trade.Category == 1)
                {
                    var o = AuctionService.GetAuctionOrder(trade.Id, trade.UserId);
                    foreach (var handler in VirtualProductHandlerManager.Current.Handlers)
                    {
                        handler.Excute(o);
                    }
                }
                
            }
            else
            {
                return;
            }
        }


        [NonAction]
        protected IOnlinePay GetOnlinePay(int platformId)
        {
            IOnlinePay pay = null;
            var platforms = GetAllPaymentPlatform();

            var p = platforms.FirstOrDefault(o => o.Id == platformId);

            if (p != null)
            {
                switch (p.Id)
                {
                    case 1: pay = new ChinaPay(p); break;
                    case 2: pay = new TenPay(p); break;
                }
            }
            return pay;
        }
    }
}